package chongwo.cn.mediaplayerproject;

import android.media.MediaRecorder;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.transition.Explode;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.map.geolocation.TencentLocation;
import com.tencent.map.geolocation.TencentLocationListener;
import com.tencent.map.geolocation.TencentLocationManager;
import com.tencent.map.geolocation.TencentLocationRequest;

import java.io.IOException;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    MediaRecorder mediaRecorder;
    String fileName;
    TextView textView;
    TextView textView2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.text);
        textView2 = (TextView) findViewById(R.id.text2);
        mediaRecorder = new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mediaRecorder.setMaxDuration(10000);
        fileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        fileName += "/"+ new Date().getTime() +".3gp";

        findViewById(R.id.startRecord).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaRecorder.setOutputFile(fileName);
                try {
                    mediaRecorder.prepare();
                } catch (IOException e) {
                    Log.i("json", "prepare() failed");
                }
                mediaRecorder.start();
            }
        });
        findViewById(R.id.stopRecord).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaRecorder.stop();
                mediaRecorder.release();
                mediaRecorder = null;
            }
        });
        findViewById(R.id.startPlayer).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    String text = Base64Coder.encodeBase64File(fileName);
                    textView.setText(text + "");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        TencentLocationRequest request = TencentLocationRequest.create();
        TencentLocationManager manager = TencentLocationManager.getInstance(getApplicationContext());
        manager.requestLocationUpdates(request, new TencentLocationListenerImpl());
    }

    class TencentLocationListenerImpl implements TencentLocationListener {

        @Override
        public void onLocationChanged(TencentLocation tencentLocation, int i, String s) {
            textView.setText(Html.fromHtml(tencentLocation.getAddress() +
                    "<br/>" + tencentLocation.getCity() + "<br />" +
                    tencentLocation.getDistrict() + "<br />" +
                    tencentLocation.getStreet() + "<br />" +
                    tencentLocation.getName() + tencentLocation.getAccuracy() + tencentLocation.getSpeed()));
        }

        @Override
        public void onStatusUpdate(String s, int i, String s1) {
            textView2.setText(s + ":" + i);
        }
    }
}